package com.unf.edu.rms.controller.category;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.unf.edu.rms.controller.userlogin.UserLoginController;
import com.unf.edu.rms.controller.users.RMSBaseController;
import com.unf.edu.rms.controller.validation.CategoryValidator;
import com.unf.edu.rms.dao.Category;
import com.unf.edu.rms.service.CategoryService;
import com.unf.edu.rms.util.RMSConstants;

@Controller
public class CategoryController extends RMSBaseController{
	@Autowired
	private CategoryService categoryService;
	@Autowired
	private UserLoginController userLoginController;
	
	@RequestMapping("/viewCategoryList")
	public String viewCategoryList(Model model) {
		
		if(userLoginController.checkValidateSession()==0 || userLoginController.checkValidateSession()==2){
			return "redirect:Dashboard.html";
		}
		List<Category> categoryList = categoryService.viewCategoryTable();
		model.addAttribute("categoryList", categoryList);
		return RMSConstants.CATEGORY_LIST_VIEW;
		
	}
	
	@RequestMapping(value = "/viewNewCategory", method = RequestMethod.POST)
	public String viewNewCategory(@ModelAttribute("category") Category category,Model model) {
		
		if(userLoginController.checkValidateSession()==0  || userLoginController.checkValidateSession()==2){
			return "redirect:Dashboard.html";
		}
		return RMSConstants.ADD_CATEGORY_VIEW;
	}
	
	@RequestMapping(value = "/addNewCategory", method = RequestMethod.POST)
	public String addNewCategory(@ModelAttribute("category") Category category,BindingResult bindingResult,Model model) {
		CategoryValidator categoryValidator = new CategoryValidator();
		categoryValidator.validate(category, bindingResult);
		
		if(!bindingResult.hasErrors()){
			categoryService.addNewCategory(category);
			List<Category> categoryList = categoryService.viewCategoryTable();
			model.addAttribute("categoryList", categoryList);
			return RMSConstants.CATEGORY_LIST_VIEW;
		}
		else{
			return RMSConstants.ADD_CATEGORY_VIEW;
		}
	}
	
	@RequestMapping(value = "/viewExistingCategory", method = RequestMethod.POST)
	public String viewExitingCategory(@RequestParam(required=true) int categoryId,Model model) {
		
		if(userLoginController.checkValidateSession()==0  || userLoginController.checkValidateSession()==2){
			return "redirect:Dashboard.html";
		}
		Category category = categoryService.getCategoryById(categoryId);
		model.addAttribute("category",category);
		return RMSConstants.ADD_CATEGORY_VIEW;
	}
	
	@RequestMapping(value = "/updateCategory", method = RequestMethod.POST)
	public String updateCategory(@RequestParam(required=true) int categoryId,@ModelAttribute("category") Category category,BindingResult bindingResult,Model model) {
		
		CategoryValidator categoryValidator = new CategoryValidator();
		categoryValidator.validate(category, bindingResult);
		
		if(!bindingResult.hasErrors()){
			category.setCategoryId(categoryId);
			categoryService.updateCategory(category);
			return "redirect:viewCategoryList.html";
		}
		else{
			return RMSConstants.ADD_CATEGORY_VIEW;
		}
	}
}
